package com.mallify.service.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mallify.common.model.cart.StoreCart;
import com.mallify.service.mapper.StoreCartMapper;
import com.mallify.service.service.StoreCartService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * StoreCartServiceImpl 接口实现
 * +----------------------------------------------------------------------
 * | HANSON [ HANSON赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Licensed HANSON并不是自由软件，未经许可不能去掉HANSON相关版权
 * +----------------------------------------------------------------------
 * | Author: HANSON Team <hanson0622@126.com>
 * +----------------------------------------------------------------------
 */
@Service
public class StoreCartServiceImpl extends ServiceImpl<StoreCartMapper, StoreCart>
        implements StoreCartService {

    @Resource
    private StoreCartMapper storeCartMapper;

    /**
     * 检测商品是否有效 更新购物车商品状态
     *
     * @param productId 商品id
     * @return 跟新结果
     */
    @Override
    public Boolean productStatusNotEnable(Integer productId) {
        StoreCart storeCartPram = new StoreCart();
        storeCartPram.setProductId(productId);
        List<StoreCart> existStoreCartProducts = getByEntity(storeCartPram);
        if (null == existStoreCartProducts) {
            return true;
        }
        existStoreCartProducts.forEach(e -> {
            e.setStatus(false);
        });
        return updateBatchById(existStoreCartProducts);
    }

    /**
     * 删除商品对应的购物车
     *
     * @param productId 商品id
     */
    @Override
    public Boolean productDelete(Integer productId) {
        // 查询购物车中是否存在与商品关联的记录
        StoreCart storeCartParm = new StoreCart();
        storeCartParm.setProductId(productId);
        List<StoreCart> existStoreCartProducts = getByEntity(storeCartParm);

        // 如果购物车中没有关联记录，直接返回 true
        if (CollUtil.isEmpty(existStoreCartProducts)) {
            return true;
        }

        // 获取购物车记录的 ID 列表
        List<Long> cartIds = existStoreCartProducts.stream().map(StoreCart::getId).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(cartIds)) {
            deleteCartByIds(cartIds);
        }

        // 如果没有需要删除的记录，返回 true
        return true;
    }

    /**
     * 删除购物车信息
     *
     * @param ids 待删除id
     * @return 删除结果状态
     */
    @Override
    public Boolean deleteCartByIds(List<Long> ids) {
        return storeCartMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 对应sku购物车生效
     * @param skuIdList skuIdList
     * @return Boolean
     */
    @Override
    public Boolean productStatusNoEnable(List<Integer> skuIdList) {
        LambdaUpdateWrapper<StoreCart> lqw = new LambdaUpdateWrapper<>();
        lqw.set(StoreCart::getStatus, true);
        lqw.in(StoreCart::getProductAttrUnique, skuIdList);
        lqw.eq(StoreCart::getIsNew, false);
        return update(lqw);
    }

    /**
     * 购物车基本查询
     *
     * @param storeCart 购物车参数
     * @return 购物车结果数据
     */
    private List<StoreCart> getByEntity(StoreCart storeCart) {
        LambdaQueryWrapper<StoreCart> lqw = new LambdaQueryWrapper<>();
        lqw.setEntity(storeCart);
        return storeCartMapper.selectList(lqw);
    }
}